package com.ctshk.sapp.hotel.APIUtils;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.ctshk.rpc.hotel.service.IHotelService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/hotel")
public class ChimeLongApi {
    @DubboReference
    private static IHotelService hotelService;
    private static final String CONTENT_WITH_NO_SIGN ="{\"mer_no\":\"9000000242\",\"method\":\"chimelong.hotel.product.info.get\",\"nonce_str\":\"80c5a87d22ed461bbe91ec691a4d9874\",\"timestamp\":\"2021-02-22 10:13:01\",\"ver_no\":\"1.0\",\"body\":\"{\\\"productCodes\\\":[\\\"G201TCTAF00003_1000000426\\\",\\\"G201BPTAF00024_1000000427\\\",\\\"G201BPTAF00023_1000000427\\\",\\\"G201TDTAF00016_1000000423\\\",\\\"G201TCTAF00004_1000000427\\\",\\\"G201TDTAF00014_1000000423\\\",\\\"G201TDTAF00015_1000000423\\\",\\\"G201TDTAF00017_1000000423\\\",\\\"G201TDTAF00018_1000000423\\\"]}\"\"}";
    //加签私钥
    private static final String PRIVATE_KEY ="MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCkC8DXIplbTY9f2ukvqny4y9kdOFV5/76GpnGX5VbjcVVrsiQRry8wA4LsoUV+YCgGx/83R19W/G0HA8dfDPbKu931x52IUgBcu/JZccJRZy5cmRC+VvY4ChL/etpiLJBG46nv1rkKlh723gzpStFgnKtVTatCkek3m38gfOo91x1+LnvrjSSlz9f6Oa5PUpPcXf7ZES/2bVCPWGB+uDMJ/nLRYQ1EEqIyFo9x4l77GYnyiuqPWrcuvr3YQ477aozpwuHJ74edch+CY8P9vOLpHCDoqA5yjTGerA6egq6FWZV+9urGjnMhWWUW3eu1LYKTuVfWxUgrB8XmPQclo7iZAgMBAAECggEAP0S5L0AM3YXgUhvf655ZU/xq1DYx4U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97pLUovnIJcBbmsFDApUobKsUJzg7uJ7vwHwaHWy+w8ErM/xocw+/GD2ODpmMCBEcK50MKFMoZEkM98uYqyFCcVjLj2faqDk9u+RL/C5eusjpuRElLTwg4NaVw==";
    //验签公钥
    private static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCtryK7CNlknTADodbiK8vR8jSQXFSwatXjwAY2XkW/tHyAT4+yrQAoqc+gJnrP51Mm+xGbH6gp6nWy+AYF7xaVqqwWM8+BjOu4hj8ZSZ3qm7A2vP7CCBDr7piXBr6GIWSygEuWhrd24oDRZya6AqZTsyPmBziTfWhqjtdBvymPNwIDAQAB";
    //请求格式
    private static final String REQUEST_FORMAT ="application/json;charset=utf-8";
    //请求路径
    private static final String REQUEST_URL_PATH = "http://47.244.225.190:8080/hub/product/ota/call2/gsa/chimelong/api.do";
    //加密key（chimelong support the key）
    private static final String SIGN_KEY  = "1d70b9997df4946824810ebb069dbe28";//长隆提供

    //调用长隆接口，将所有酒店信息存入数据库
    public static void getAllHotelOfChimeLong(){
        String toData = null;
        String param = new APISignUtil().contentJointTest(CONTENT_WITH_NO_SIGN);
        toData = new APIAesUtil().aesEncodeBase64(param, SIGN_KEY);
        String jsonToReport =
                "{" +
                        "\"mer_no\":\"9000000242\","+
                        "\"data\":\""+toData+"\""+
                        "}";
        String result = new APIAesUtil().aesDecodeBase64(new APIUtil().callAPIFunc(REQUEST_FORMAT, REQUEST_URL_PATH, jsonToReport,"post"), SIGN_KEY);
        System.out.println(result);
        JSONObject j = new JSONObject(result);
//        .getJSONArray("hotelData").getJSONObject(0).getJSONArray("hotelCode").getJSONObject(0).get("value").toString()
        JSONArray hotelDate = j.getJSONObject("body").getJSONArray("hotelData");
        List<HotelData> parse = (List<HotelData>) JSON.parseArray(String.valueOf(hotelDate), HotelData.class);
//        for (int i = 0; i <parse.size() ; i++) {
//            String address = parse.get(i).getAddress();
//            HotelReq hotelReq = new HotelReq();
//            JSONObject jsonHotelName = new JSONObject(parse.get(i).getHotelName());
//            hotelReq.setEnName(jsonHotelName.get("en").toString());
//            hotelReq.setName(jsonHotelName.get("zh").toString());
//            JSONObject jsonHotelAddress = new JSONObject(parse.get(i).getAddress());
//            hotelReq.setStarLevel(parse.get(i).getStarLevel());
//            parse.get(i).getHotelCode();
//            parse.get(i).getOperatingStatus();
//            hotelReq.setTelephone(parse.get(i).getPhone());
//            hotelReq.setLocation(jsonHotelAddress.get("zh").toString());
//            hotelService.saveHotel(hotelReq,null);
//        }
    }
    public static void main(String[] args) {
        getAllHotelOfChimeLong();
    }
}
